﻿using UnityEditor;
using UnityEngine;

public class BundleGroupTools : EditorWindow
{
    static BundleGroup m_BundleGroup = null;
    Vector2 m_Scroll = Vector2.zero;

    static bool m_IsClearPer = true;
    static int index = 0;

    static public void NewBundle(BundleGroup bundle)
    {
        BundleGroupTools window = GetWindow<BundleGroupTools>(false, "New Bundle", true);
        window.autoRepaintOnSceneChange = true;
        if (bundle != null)
        {
            m_BundleGroup = new BundleGroup(bundle);
        }
        else
        {
            m_BundleGroup = bundle;
        }

        Selection.activeObject = null;
    }

    void OnSelectionChange()
    {
        SelectedObjects();
        Repaint();
    }

    void OnGUI()
    {
        EditorGUIUtility.labelWidth = 100f;

        if (m_BundleGroup == null)
        {
            m_BundleGroup = new BundleGroup();
        }
        EditorGUILayout.LabelField("Create New AssetBundle: ");

        BuildAssetBundle.DrawSeparator();
        m_BundleGroup.m_BundleName = EditorGUILayout.TextField("BundleName *", m_BundleGroup.m_BundleName);
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("BundleOutFolder * ../AssetBundles/", GUILayout.Width(210f));
        m_BundleGroup.m_BundlePath = EditorGUILayout.TextField(m_BundleGroup.m_BundlePath);

        if (BuildAssetBundle.m_OutPutFolder != null && BuildAssetBundle.m_OutPutFolder.Count > 0)
        {
            index = EditorGUILayout.Popup(index, BuildAssetBundle.m_OutPutFolder.ToArray(), GUILayout.Width(25f));
            if (index >= 0)
            {
                m_BundleGroup.m_BundlePath = BuildAssetBundle.m_OutPutFolder[index];
                index = -1;
                GUI.FocusControl("UpdateButton");
            }
        }

        EditorGUILayout.EndHorizontal();
        //m_BundleGroup.m_MainAsset = EditorGUILayout.ObjectField("MainAsset", m_BundleGroup.m_MainAsset, typeof(Object), false);
        m_BundleGroup.m_BundleType = (BundleType)EditorGUILayout.EnumPopup("BundleType *", m_BundleGroup.m_BundleType);
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Module", GUILayout.Width(140f));
        m_BundleGroup.m_Module = EditorGUILayout.TextField(m_BundleGroup.m_Module);
        if (string.IsNullOrEmpty(m_BundleGroup.m_Module))
        {
            m_BundleGroup.m_Module = m_BundleGroup.m_BundleName;
        }

        EditorGUILayout.EndHorizontal();
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Clear After Add/Update All");
        m_IsClearPer = EditorGUILayout.Toggle("", m_IsClearPer);
        EditorGUILayout.EndHorizontal();

        GUI.enabled = m_BundleGroup.m_AssetArray.Count > 0 ? true : false;
        GUI.SetNextControlName("UpdateButton");
        if (GUILayout.Button("Add/Update All"))
        {
            BuildAssetBundle.AddBundle(m_BundleGroup);
            if (m_IsClearPer)
            {
                NewBundle(null);
            }
        }
        BuildAssetBundle.DrawSeparator();
        GUI.enabled = true;

        BuildAssetBundle.HighlightLine(Color.black, 20f);
        EditorGUILayout.LabelField("Assets", EditorStyles.boldLabel);

        m_Scroll = EditorGUILayout.BeginScrollView(m_Scroll);

        if (m_BundleGroup != null && m_BundleGroup.m_AssetArray.Count > 0)
        {
            if (string.IsNullOrEmpty(m_BundleGroup.m_BundleName))
            {
                m_BundleGroup.m_BundleName = m_BundleGroup.m_AssetArray[0].name;
            }

            for (int i = m_BundleGroup.m_AssetArray.Count - 1; i >= 0; i--)
            {
                Object o = m_BundleGroup.m_AssetArray[i];
                BuildAssetBundle.HighlightLine(Color.white, 25f);
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(o.name);
                EditorGUILayout.LabelField(GetObjectType(o), GUILayout.Width(100f));
                if (GUILayout.Button("Delete", GUILayout.Width(50f)))
                {
                    m_BundleGroup.m_AssetArray.RemoveAt(i);
                }
                EditorGUILayout.EndHorizontal();
            }
        }

        EditorGUILayout.EndScrollView();
    }

    void SelectedObjects()
    {
        if (Selection.objects != null && Selection.objects.Length > 0)
        {
            Object[] objects = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);

            foreach (Object o in objects)
            {
                if (m_BundleGroup != null && !m_BundleGroup.m_AssetArray.Contains(o))
                {
                    m_BundleGroup.m_AssetArray.Add(o);
                }
            }
        }
    }

    string GetObjectType(Object o)
    {
        string type = o.GetType().ToString();
        return type.Substring(12);
    }
}